package Machine;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import javax.management.monitor.Monitor;
import javax.tools.Diagnostic;

import Monitoring_Machine.Type_commande;

import com.jezhumble.javasysmon.CpuTimes;
import com.jezhumble.javasysmon.JavaSysMon;
import com.jezhumble.javasysmon.MemoryStats;


//import sun.net.www.content.text.plain;

public class Commande {

public Commande()
{
	
}
        
public static void main(String[] args) {
	 
	 String cmd="ps";

	 String[] commands = new String[]{"grep", "hello world", 
	 "c:\\Documents and Settings\\f.txt"};
	 
	 try {
	         
	        Runtime r = Runtime.getRuntime();
	        Process p = r.exec(cmd);
	        p.waitFor();
	        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
	        String inputLine;
	        while ((inputLine = in.readLine()) != null) System.out.println(inputLine); // Retourne Le diagnostic
	        System.out.println("availableProcessors : " + r.availableProcessors()); // Nombre de processeurs sous-jacent
	        System.out.println("freeMemory : " + r.freeMemory()); // Taux de m�moire libre
	        System.out.println("maxMemory : " + r.maxMemory()); // Taux Max de m�moire
	        System.out.println("totalMemory : " + r.totalMemory()); // Taux Total de m�moire 
	        System.out.println(System.getProperties().get("os.name")); // Pour savoir l'OS sous-jacent
	        System.out.println(System.getProperty("java.version")); // version de JAVA
	
	 }catch(Exception e) {
	         System.out.println("erreur d'execution " +e.toString());
	       }

	 
	     }
public Monitoring_Machine.Diagnostic executer_cmd(Type_commande cmd){
	
    Monitoring_Machine.Diagnostic diagnostic= new Monitoring_Machine.Diagnostic(cmd,"",0);
	
	switch (cmd.value()) {
	case Type_commande._cpu:
		diagnostic=executer_cpu();
		break;
	
    case Type_commande._mem:
		diagnostic=executer_mem();
		break;
		
    case Type_commande._os:
    	
    	diagnostic.resultat=(String) System.getProperties().get("os.name");
    	
    	break;
    	
    case Type_commande._programme:
    	diagnostic.resultat=System.getProperty("java.version");
    	break;

	default:
		break;
	}
	return diagnostic;
	}


public Monitoring_Machine.Diagnostic executer_cpu()
{
	
    return null;	
}


public Monitoring_Machine.Diagnostic executer_mem()
{
	return null;
}

public static long Free_Disk() throws IOException
{
	Process leProcessFils = null;
	    String res="";
	        Process proc = null;
	          proc = Runtime.getRuntime().exec("cmd /c dir/tc "+res);
	          InputStream is = proc.getInputStream();
	          BufferedReader br = new BufferedReader(new InputStreamReader(is));
	          String enCour,der="";
	          while ( (enCour = br.readLine()) != null) {
	            der=enCour;
	          }
	        res=der.replaceAll("�"," ");
	        int pos1=res.indexOf(")  ");
	        int pos2=res.indexOf(" o");
	        return (long)Long.parseLong(res.substring(pos1+3,pos2));
	 
	  }


 } 

